package com.song.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.dao.NetClassDao;
import com.song.dao.PermissionDao;
import com.song.dao.RoleDao;
import com.song.dao.UserDao;
import com.song.po.Permission;
import com.song.po.Role;
import com.song.po.User;
import com.song.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
   @Autowired
   private UserDao userDao;
   @Autowired
   private NetClassDao netClassDao;
   @Autowired
   private RoleDao roleDao;
   @Autowired
   private PermissionDao  permissionDao;

   @Override
   public User getUser(String userName, String password) {
      QueryWrapper<User> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("user_name",userName);
      queryWrapper.eq("password",password);
      User user = userDao.selectOne(queryWrapper);
      if(user != null){
         QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
         queryWrapper1.eq("ur.user_id",user.getId());
         Role role = roleDao.getRole(queryWrapper1);
         if(role != null){
            user.setRole(role);
            QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("rp.role_id",role.getRoleId());
            List<Permission> list = permissionDao.getPermissionList(queryWrapper2);
            if(list != null){
               user.setPermissionList(list);
            }
         }
      }
      return user;
   }


}
